package com.bjpowernode.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.constant.SystemConstant;
import com.bjpowernode.boss.convert.UserInfoConvert;
import com.bjpowernode.boss.enums.RespInfo;
import com.bjpowernode.boss.exception.PowerException;
import com.bjpowernode.boss.mvc.models.query.UserEditQuery;
import com.bjpowernode.boss.mvc.models.response.AjaxResult;
import com.bjpowernode.boss.mvc.service.UserService;
import com.bjpowernode.boss.persist.domain.UserInfo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserInfoConvert userInfoConvert;

    @GetMapping("/user/list/{pageNo}")
    public AjaxResult getUserList(@PathVariable Integer pageNo) {
        Page<UserInfo> page = userService.getUserList(pageNo, SystemConstant.PAGE_SIZE);
        List<UserInfo> userData = page.getRecords();
        long total = page.getTotal();
        long size = page.getSize();
        Map<String, Object> data = new HashMap<>();
        data.put("userData", userInfoConvert.userInfoListToUserInfoVoList(userData));
        data.put("total", total);
        data.put("pageSize", size);
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        return AjaxResult.OK(data);
    }

//    @PostMapping("/user/edit")
//    public AjaxResult editUser(@RequestBody ) {
//
//    }

    @PostMapping("/user/edit")
    public AjaxResult editUser(@RequestBody @Valid UserEditQuery userEditQuery) {
        userService.edit(userEditQuery);
        return AjaxResult.OK();
    }

    @GetMapping("/user/queryByUserNo/{userNo}")
    public AjaxResult queryByUserNo(@PathVariable String userNo) {
        if (StrUtil.isNotBlank(userNo)) {
            UserInfo userInfo = userService.queryByUserNo(userNo);
            if (userInfo == null) {
                throw new PowerException(RespInfo.USER_NOT_EXIST_ERROR);
            }
            return AjaxResult.OK(userInfoConvert.userInfoToUserInfoVo(userInfo));
        } else {
            throw new PowerException(RespInfo.REQUEST_PARAM_ERROR);
        }
    }

    @GetMapping("/user/status")
    public AjaxResult changeUserStatus(UserEditQuery userEditQuery) {
        userService.changeStatus(userEditQuery);
        return AjaxResult.OK();
    }

    /*模糊匹配商户名称*/
    @GetMapping("/user/matchShopName/{shopName}")
    public AjaxResult matchShopName(@PathVariable String shopName) {
        if (StrUtil.isNotBlank(shopName) && shopName.trim().length() >= 2) {
            List<UserInfo> userInfoList = userService.matchShopName(shopName);
            return AjaxResult.OK(userInfoConvert.userInfoListToShopNameVoList(userInfoList));
        }
        throw new PowerException(RespInfo.REQUEST_PARAM_ERROR);
    }

}
